package com.ssy.lingxi.report.serviceimpl.tally;

import com.ssy.lingxi.report.api.enums.EnhanceProcessOperateTypeEnum;
import com.ssy.lingxi.report.api.enums.EnhanceSupplierOperateTypeEnum;
import com.ssy.lingxi.report.api.enums.OperateDataSourceEnum;
import com.ssy.lingxi.report.api.vo.request.OperateNoticeVO;
import com.ssy.lingxi.report.dao.enhance.ProduceNoticeOrderDAO;
import com.ssy.lingxi.report.entity.report.MemberTallyReportDO;
import com.ssy.lingxi.report.model.bo.MemberTallyReportBO;
import com.ssy.lingxi.report.model.bo.StatisticsCountBO;
import com.ssy.lingxi.report.model.bo.tally.EnhanceProcessTallyBO;
import com.ssy.lingxi.report.model.bo.tally.EnhanceSupplierTallyBO;
import com.ssy.lingxi.report.model.constant.enhance.PNOInnerStatusEnum;
import com.ssy.lingxi.report.model.vo.response.EnhanceTallyReportVO;
import com.ssy.lingxi.report.model.vo.response.TallyReportItemVO;
import com.ssy.lingxi.report.service.tally.IEnhanceTallyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 加工计数统计接口实现
 * @author 伍将
 * @date 2020/11/23
 * @version 2.0.0
 */
@Service
public class EnhanceTallyServiceImpl implements IEnhanceTallyService {

    private static final Logger logger = LoggerFactory.getLogger(EnhanceTallyServiceImpl.class);

    @Autowired
    private ProduceNoticeOrderDAO produceNoticeOrderDAO;

    /**
     * 转换物流
     * @param reportBO
     * @return
     */
    @Override
    public EnhanceTallyReportVO convertEnhance(MemberTallyReportBO reportBO) {
        EnhanceTallyReportVO result = new EnhanceTallyReportVO();
        if (reportBO == null) {
            reportBO = new MemberTallyReportBO();
        }
        result.setSupplierList(convertEnhanceSupplierTally(reportBO.getEnhanceSupplier()));
        result.setProcessList(convertEnhanceProcessTally(reportBO.getEnhanceProcess()));

        return result;
    }

    /**
     * 设置加工
     * @param tallyReportO
     * @param noticeVO
     * @return
     */
    @Override
    public MemberTallyReportBO setEnhanceTally(MemberTallyReportBO tallyReportO, OperateNoticeVO noticeVO) {
        OperateDataSourceEnum sourceEnum = OperateDataSourceEnum.getEnum(noticeVO.getDataSource());
        switch (sourceEnum) {
            case ENHANCE_PROCESS: // 确认生产通知单
                tallyReportO.setEnhanceProcess(setEnhanceProcessTally(tallyReportO.getEnhanceProcess(), noticeVO));
                break;
            case ENHANCE_SUPPLIER: // 指派生产通知单
                tallyReportO.setEnhanceSupplier(setEnhanceSupplierTally(tallyReportO.getEnhanceSupplier(), noticeVO));
                break;
        }
        return tallyReportO;
    }


    /**
     * 转换生产通知单处理
     * @param tallyBO
     * @return
     */
    private List<TallyReportItemVO> convertEnhanceProcessTally(EnhanceProcessTallyBO tallyBO) {
        List<TallyReportItemVO> resultList = new ArrayList<>();
        if (tallyBO == null) {
            tallyBO = new EnhanceProcessTallyBO();
        }
        // 待提交审核生产通知单
        TallyReportItemVO tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeCommitValifyCount());
        tallyReportItem.setName(EnhanceProcessOperateTypeEnum.TO_BE_VALIFY_COMMIT.getName());
        tallyReportItem.setLink(EnhanceProcessOperateTypeEnum.TO_BE_VALIFY_COMMIT.getLink());
        resultList.add(tallyReportItem);
        // 待审核生产通知单（一级）
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeValifyStep1Count());
        tallyReportItem.setName(EnhanceProcessOperateTypeEnum.TO_BE_VALIFY_STEP1.getName());
        tallyReportItem.setLink(EnhanceProcessOperateTypeEnum.TO_BE_VALIFY_STEP1.getLink());
        resultList.add(tallyReportItem);
        // 待审核生产通知单（二级）
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeValifyStep2Count());
        tallyReportItem.setName(EnhanceProcessOperateTypeEnum.TO_BE_VALIFY_STEP2.getName());
        tallyReportItem.setLink(EnhanceProcessOperateTypeEnum.TO_BE_VALIFY_STEP2.getLink());
        resultList.add(tallyReportItem);
        // 待确认生产通知单
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeConfirmCount());
        tallyReportItem.setName(EnhanceProcessOperateTypeEnum.TO_BE_CONFIRM.getName());
        tallyReportItem.setLink(EnhanceProcessOperateTypeEnum.TO_BE_CONFIRM.getLink());
        resultList.add(tallyReportItem);
        // 待新增加工发货单
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeAddDeliveryGoodsCount());
        tallyReportItem.setName(EnhanceProcessOperateTypeEnum.TO_BE_ADD_DELIVERY.getName());
        tallyReportItem.setLink(EnhanceProcessOperateTypeEnum.TO_BE_ADD_DELIVERY.getLink());
        resultList.add(tallyReportItem);
        // 待新增物流单
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeAddLogisticsCount());
        tallyReportItem.setName(EnhanceProcessOperateTypeEnum.TO_BE_ADD_LOGISTICS.getName());
        tallyReportItem.setLink(EnhanceProcessOperateTypeEnum.TO_BE_ADD_LOGISTICS.getLink());
        resultList.add(tallyReportItem);
        // 待发货生产通知单
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeDeliveryGoodsCount());
        tallyReportItem.setName(EnhanceProcessOperateTypeEnum.TO_BE_DELIVERY.getName());
        tallyReportItem.setLink(EnhanceProcessOperateTypeEnum.TO_BE_DELIVERY.getLink());
        resultList.add(tallyReportItem);
        // 待确认回单生产通知单
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeConfirmReceiptCount());
        tallyReportItem.setName(EnhanceProcessOperateTypeEnum.TO_BE_CONFIRM_RECEIPT.getName());
        tallyReportItem.setLink(EnhanceProcessOperateTypeEnum.TO_BE_CONFIRM_RECEIPT.getLink());
        resultList.add(tallyReportItem);

        resultList.forEach(o -> o.setCount(o.getCount() > 0 ? o.getCount() : 0));

        return resultList;
    }

    /**
     * 转换指派生产通知单
     * @param tallyBO
     * @return
     */
    private List<TallyReportItemVO> convertEnhanceSupplierTally(EnhanceSupplierTallyBO tallyBO) {
        List<TallyReportItemVO> resultList = new ArrayList<>();
        if (tallyBO == null) {
            tallyBO = new EnhanceSupplierTallyBO();
        }
        // 待提交审核数
        TallyReportItemVO tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeCommitValifyCount());
        tallyReportItem.setName(EnhanceSupplierOperateTypeEnum.TO_BE_VALIFY_COMMIT.getName());
        tallyReportItem.setLink(EnhanceSupplierOperateTypeEnum.TO_BE_VALIFY_COMMIT.getLink());
        resultList.add(tallyReportItem);
        // 待一级审核
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeValifyStep1Count());
        tallyReportItem.setName(EnhanceSupplierOperateTypeEnum.TO_BE_VALIFY_STEP1.getName());
        tallyReportItem.setLink(EnhanceSupplierOperateTypeEnum.TO_BE_VALIFY_STEP1.getLink());
        resultList.add(tallyReportItem);
        // 待二级审核
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeValifyStep2Count());
        tallyReportItem.setName(EnhanceSupplierOperateTypeEnum.TO_BE_VALIFY_STEP2.getName());
        tallyReportItem.setLink(EnhanceSupplierOperateTypeEnum.TO_BE_VALIFY_STEP2.getLink());
        resultList.add(tallyReportItem);
        // 待提交生产通知单数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeCommitCount());
        tallyReportItem.setName(EnhanceSupplierOperateTypeEnum.TO_BE_COMMIT.getName());
        tallyReportItem.setLink(EnhanceSupplierOperateTypeEnum.TO_BE_COMMIT.getLink());
        resultList.add(tallyReportItem);
        // 待待新增加工入库单数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeAddStorageCount());
        tallyReportItem.setName(EnhanceSupplierOperateTypeEnum.TO_BE_ADD_STORAGE.getName());
        tallyReportItem.setLink(EnhanceSupplierOperateTypeEnum.TO_BE_ADD_STORAGE.getLink());
        resultList.add(tallyReportItem);
        // 待收货生产通知单数
        tallyReportItem = new TallyReportItemVO();
        tallyReportItem.setCount(tallyBO.getToBeReceiveCount());
        tallyReportItem.setName(EnhanceSupplierOperateTypeEnum.TO_BE_RECEIVE.getName());
        tallyReportItem.setLink(EnhanceSupplierOperateTypeEnum.TO_BE_RECEIVE.getLink());
        resultList.add(tallyReportItem);

        resultList.forEach(o -> o.setCount(o.getCount() > 0 ? o.getCount() : 0));

        return resultList;
    }

    /**
     * 设置指派生产通知单
     * @param tallyBO
     * @param noticeVO
     * @return
     */
    private EnhanceSupplierTallyBO setEnhanceSupplierTally(EnhanceSupplierTallyBO tallyBO, OperateNoticeVO noticeVO) {
        if (tallyBO == null) {
            tallyBO = new EnhanceSupplierTallyBO();
        }
        // 更新数据
        EnhanceSupplierOperateTypeEnum typeEnum;
        if (noticeVO.getReduceCount() > 0) {
            typeEnum = EnhanceSupplierOperateTypeEnum.getEnum(noticeVO.getReduceOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_VALIFY_COMMIT:
                        tallyBO.setToBeCommitValifyCount(tallyBO.getToBeCommitValifyCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_VALIFY_STEP1:
                        tallyBO.setToBeValifyStep1Count(tallyBO.getToBeValifyStep1Count() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_VALIFY_STEP2:
                        tallyBO.setToBeValifyStep2Count(tallyBO.getToBeValifyStep2Count() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_COMMIT:
                        tallyBO.setToBeCommitCount(tallyBO.getToBeCommitCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_ADD_STORAGE:
                        tallyBO.setToBeAddStorageCount(tallyBO.getToBeAddStorageCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_RECEIVE:
                        tallyBO.setToBeReceiveCount(tallyBO.getToBeReceiveCount() - noticeVO.getReduceCount());
                        break;
                }
            }
        }
        if (noticeVO.getIncreaseCount() > 0) {
            typeEnum = EnhanceSupplierOperateTypeEnum.getEnum(noticeVO.getIncreaseOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_VALIFY_COMMIT:
                        tallyBO.setToBeCommitValifyCount(tallyBO.getToBeCommitValifyCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_VALIFY_STEP1:
                        tallyBO.setToBeValifyStep1Count(tallyBO.getToBeValifyStep1Count() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_VALIFY_STEP2:
                        tallyBO.setToBeValifyStep2Count(tallyBO.getToBeValifyStep2Count() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_COMMIT:
                        tallyBO.setToBeCommitCount(tallyBO.getToBeCommitCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_ADD_STORAGE:
                        tallyBO.setToBeAddStorageCount(tallyBO.getToBeAddStorageCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_RECEIVE:
                        tallyBO.setToBeReceiveCount(tallyBO.getToBeReceiveCount() + noticeVO.getIncreaseCount());
                        break;
                }
            }
        }

        return tallyBO;
    }

    /**
     * 设置生产通知单处理
     * @param tallyBO
     * @param noticeVO
     * @return
     */
    private EnhanceProcessTallyBO setEnhanceProcessTally(EnhanceProcessTallyBO tallyBO, OperateNoticeVO noticeVO) {
        if (tallyBO == null) {
            tallyBO = new EnhanceProcessTallyBO();
        }
        // 更新数据
        EnhanceProcessOperateTypeEnum typeEnum;
        if (noticeVO.getReduceCount() > 0) {
            typeEnum = EnhanceProcessOperateTypeEnum.getEnum(noticeVO.getReduceOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_VALIFY_COMMIT:
                        tallyBO.setToBeCommitValifyCount(tallyBO.getToBeCommitValifyCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_VALIFY_STEP1:
                        tallyBO.setToBeValifyStep1Count(tallyBO.getToBeValifyStep1Count() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_VALIFY_STEP2:
                        tallyBO.setToBeValifyStep2Count(tallyBO.getToBeValifyStep2Count() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_CONFIRM:
                        tallyBO.setToBeConfirmCount(tallyBO.getToBeConfirmCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_ADD_DELIVERY:
                        tallyBO.setToBeAddDeliveryGoodsCount(tallyBO.getToBeAddDeliveryGoodsCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_ADD_LOGISTICS:
                        tallyBO.setToBeAddLogisticsCount(tallyBO.getToBeAddLogisticsCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_CONFIRM_RECEIPT:
                        tallyBO.setToBeConfirmReceiptCount(tallyBO.getToBeConfirmReceiptCount() - noticeVO.getReduceCount());
                        break;
                    case TO_BE_DELIVERY:
                        tallyBO.setToBeDeliveryGoodsCount(tallyBO.getToBeDeliveryGoodsCount() - noticeVO.getReduceCount());
                        break;
                }
            }
        }
        if (noticeVO.getIncreaseCount() > 0) {
            typeEnum = EnhanceProcessOperateTypeEnum.getEnum(noticeVO.getIncreaseOperateType());
            if (typeEnum != null) {
                switch (typeEnum) {
                    case TO_BE_VALIFY_COMMIT:
                        tallyBO.setToBeCommitValifyCount(tallyBO.getToBeCommitValifyCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_VALIFY_STEP1:
                        tallyBO.setToBeValifyStep1Count(tallyBO.getToBeValifyStep1Count() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_VALIFY_STEP2:
                        tallyBO.setToBeValifyStep2Count(tallyBO.getToBeValifyStep2Count() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_CONFIRM:
                        tallyBO.setToBeConfirmCount(tallyBO.getToBeConfirmCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_ADD_DELIVERY:
                        tallyBO.setToBeAddDeliveryGoodsCount(tallyBO.getToBeAddDeliveryGoodsCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_ADD_LOGISTICS:
                        tallyBO.setToBeAddLogisticsCount(tallyBO.getToBeAddLogisticsCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_CONFIRM_RECEIPT:
                        tallyBO.setToBeConfirmReceiptCount(tallyBO.getToBeConfirmReceiptCount() + noticeVO.getIncreaseCount());
                        break;
                    case TO_BE_DELIVERY:
                        tallyBO.setToBeDeliveryGoodsCount(tallyBO.getToBeDeliveryGoodsCount() + noticeVO.getIncreaseCount());
                        break;
                }
            }
        }

        return tallyBO;
    }

    /**
     * 统计计数
     * @author 伍将
     * @date 2020/11/26
     * @param reportList: 
     * @return:
     **/
    @Override
    public List<MemberTallyReportDO> statisticsTally(List<MemberTallyReportDO> reportList) {
        // 生产通知单处理
        reportList = statisticsProcessTally(reportList);

        // 指派生产通知单
        reportList = statisticsSupplierTally(reportList);

        return reportList;
    }

    /**
     * 指派生产通知单
     * @author 伍将
     * @date 2020/11/26
     * @param reportList:
     * @return:
     **/
    private List<MemberTallyReportDO> statisticsSupplierTally(List<MemberTallyReportDO> reportList) {
        if (reportList == null) {
            reportList = new ArrayList<>();
        }
        List<StatisticsCountBO> statisticsList = produceNoticeOrderDAO.getSupplierStatisticsCount();
        // 补全统计数据
        List<MemberTallyReportDO> finalReportList = replenishData(reportList, statisticsList, "确认生产通知单");

        List<StatisticsCountBO> finalStatisticsList = statisticsList;
        finalReportList.forEach(p -> {
            // 如果报表数据为null，则实例化
            if (p.getTallyReport() == null) {
                p.setTallyReport(new MemberTallyReportBO());
            }
            EnhanceSupplierTallyBO tallyBO = new EnhanceSupplierTallyBO();
            // 待提交审核生产通知单数
            tallyBO.setToBeCommitValifyCount(finalStatisticsList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())
                            && o.getStatusType().equals(PNOInnerStatusEnum.TO_BE_SUPPLIER_SUBMIT_EXAM.getCode()))
                    .mapToLong(StatisticsCountBO::getCount).sum());
            // 待审核生产通知单（一级）
            tallyBO.setToBeValifyStep1Count(finalStatisticsList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())
                            && o.getStatusType().equals(PNOInnerStatusEnum.TO_BE_SUPPLIER_EXAM1.getCode()))
                    .mapToLong(StatisticsCountBO::getCount).sum());
            // 待审核生产通知单（二级）
            tallyBO.setToBeValifyStep2Count(finalStatisticsList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())
                            && o.getStatusType().equals(PNOInnerStatusEnum.TO_BE_SUPPLIER_EXAM2.getCode()))
                    .mapToLong(StatisticsCountBO::getCount).sum());
            // 待提交生产通知单数
            tallyBO.setToBeCommitCount(finalStatisticsList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())
                            && o.getStatusType().equals(PNOInnerStatusEnum.TO_BE_SUPPLIER_SUBMIT.getCode()))
                    .mapToLong(StatisticsCountBO::getCount).sum());
            // 待新增加工入库单数
            tallyBO.setToBeAddStorageCount(finalStatisticsList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())
                            && o.getStatusType().equals(PNOInnerStatusEnum.TO_BE_SUPPLIER_ADD_STORAGE.getCode()))
                    .mapToLong(StatisticsCountBO::getCount).sum());
            // 待收货生产通知单数
            tallyBO.setToBeReceiveCount(finalStatisticsList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())
                            && o.getStatusType().equals(PNOInnerStatusEnum.TO_BE_SUPPLIER_CONFIRM_RECEIVE.getCode()))
                    .mapToLong(StatisticsCountBO::getCount).sum());
            p.getTallyReport().setEnhanceSupplier(tallyBO);
        });

        return finalReportList;
    }

    /**
     * 生产通知单处理
     * @author 伍将
     * @date 2020/11/26
     * @param reportList:
     * @return:
     **/
    private List<MemberTallyReportDO> statisticsProcessTally(List<MemberTallyReportDO> reportList) {
        if (reportList == null) {
            reportList = new ArrayList<>();
        }
        List<StatisticsCountBO> statisticsList = produceNoticeOrderDAO.getProcessStatisticsCount();
        // 补全统计数据
        List<MemberTallyReportDO> finalReportList = replenishData(reportList, statisticsList, "确认生产通知单");

        List<StatisticsCountBO> finalStatisticsList = statisticsList;
        finalReportList.forEach(p -> {
            // 如果报表数据为null，则实例化
            if (p.getTallyReport() == null) {
                p.setTallyReport(new MemberTallyReportBO());
            }
            EnhanceProcessTallyBO tallyBO = new EnhanceProcessTallyBO();
            // 待提交审核生产通知单数
            tallyBO.setToBeCommitValifyCount(finalStatisticsList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())
                            && o.getStatusType().equals(PNOInnerStatusEnum.TO_BE_PROCESS_SUBMIT_EXAM.getCode()))
                    .mapToLong(StatisticsCountBO::getCount).sum());
            // 待审核生产通知单（一级）
            tallyBO.setToBeValifyStep1Count(finalStatisticsList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())
                            && o.getStatusType().equals(PNOInnerStatusEnum.TO_BE_PROCESS_EXAM1.getCode()))
                    .mapToLong(StatisticsCountBO::getCount).sum());
            // 待审核生产通知单（二级）
            tallyBO.setToBeValifyStep2Count(finalStatisticsList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())
                            && o.getStatusType().equals(PNOInnerStatusEnum.TO_BE_PROCESS_EXAM2.getCode()))
                    .mapToLong(StatisticsCountBO::getCount).sum());
            // 待确认生产通知单数
            tallyBO.setToBeConfirmCount(finalStatisticsList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())
                            && o.getStatusType().equals(PNOInnerStatusEnum.TO_BE_PROCESS_CONFIRM.getCode()))
                    .mapToLong(StatisticsCountBO::getCount).sum());
            // 待新增加工发货单数
            tallyBO.setToBeAddDeliveryGoodsCount(finalStatisticsList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())
                            && o.getStatusType().equals(PNOInnerStatusEnum.TO_BE_PROCESS_ADD_DELIVERY.getCode()))
                    .mapToLong(StatisticsCountBO::getCount).sum());
            // 待新增物流单数
            tallyBO.setToBeAddLogisticsCount(finalStatisticsList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())
                            && o.getStatusType().equals(PNOInnerStatusEnum.TO_BE_PROCESS_ADD_LOGISTICS.getCode()))
                    .mapToLong(StatisticsCountBO::getCount).sum());
            // 待发货生产通知单数
            tallyBO.setToBeDeliveryGoodsCount(finalStatisticsList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())
                            && o.getStatusType().equals(PNOInnerStatusEnum.TO_BE_PROCESS_CONFIRM_DELIVERY.getCode()))
                    .mapToLong(StatisticsCountBO::getCount).sum());
            // 待确认回单生产通知单数
            tallyBO.setToBeConfirmReceiptCount(finalStatisticsList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId())
                            && o.getStatusType().equals(PNOInnerStatusEnum.TO_BE_PROCESS_CONFIRM_RECEIPT.getCode()))
                    .mapToLong(StatisticsCountBO::getCount).sum());

            p.getTallyReport().setEnhanceProcess(tallyBO);
        });

        return finalReportList;
    }

    /**
     * 补全数据
     * @param reportList
     * @param statisticsList
     * @param name
     * @return
     */
    private List<MemberTallyReportDO> replenishData(List<MemberTallyReportDO> reportList, List<StatisticsCountBO> statisticsList, String name) {
        if (statisticsList == null) {
            logger.info("没有符合条件的{}计数统计数据", name);
            statisticsList = new ArrayList<>();
        }

        // 补全统计数据
        List<MemberTallyReportDO> finalReportList = reportList;
        statisticsList.stream().distinct().forEach(p -> {
            MemberTallyReportDO reportDO = finalReportList.stream()
                    .filter(o -> o.getMemberId().equals(p.getMemberId()) && o.getRoleId().equals(p.getRoleId()))
                    .findFirst().orElse(null);
            if (reportDO == null) {
                reportDO = new MemberTallyReportDO();
                reportDO.setMemberId(p.getMemberId());
                reportDO.setRoleId(p.getRoleId());
                finalReportList.add(reportDO);
            }
        });

        return finalReportList;
    }
}
